var Tasklog=Tasklog||{}

// renders a colorpicker to the target element. callback returns selected colour
// getColor() - returns currently selected colour
// setColor(index || hex) - sets the colour by array index or by hex color name 
// usage:
// new Tasklog.Colorpicker({ target: {$element}, defaultColor: {number}, scope: {object}, callback: {function} });
Tasklog.Colorpicker=(function(confO){
	var 
		cp={conf:{}},
		colors=[
			'#000000','#444444','#666666','#999999','#CCCCCC','#EEEEEE','#F3F3F3','#FFFFFF', '-',
			'#FF0000','#FF9900','#FFFF00','#00FF00','#00FFFF','#0000FF','#9900FF','#FF00FF', '-',
			'#F4CCCC','#FCE5CD','#FFF2CC','#D9EAD3','#D0E0E3','#CFE2F3','#D9D2E9','#EAD1DC',
			'#EA9999','#F9CB9C','#FFE599','#B6D7A8','#A2C4C9','#9FC5E8','#B4A7D6','#D5A6BD',
			'#E06666','#F6B26B','#FFD966','#93C47D','#76A5AF','#6FA8DC','#8E7CC3','#C27BA0',
			'#CC0000','#E69138','#F1C232','#6AA84F','#45818E','#3D85C6','#674EA7','#A64D79',
			'#990000','#B45F06','#BF9000','#38761D','#134F5C','#0B5394','#351C75','#741B47',
			'#660000','#783F04','#7F6000','#274E13','#0C343D','#073763','#20124D','#4C1130'
		],
		conf = { target: $('body'), defaultColor: 0, columns: 0, scope: cp, callback: function(){} }, clen=colors.length, _html='';
		
	function isdark(color){var colr=parseInt(color.substr(1),16); return (colr>>>16)+((colr>>>8)&0x00ff)+(colr&0x0000ff)<500;}
	function rgb(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
	function toHex(N){if(N==null)return "00";var chr="0123456789ABCDEF";N=parseInt(N);if(N==0||isNaN(N))return "00";N=Math.max(0,N);N=Math.min(N,255);N=Math.round(N);return chr.charAt((N-N%16)/16)+chr.charAt(N%16);}
	
	$.extend(cp.conf, conf, confO);
	
	cp.setColor=function(col){
		if (!col||col=='transparent') return;
		cp.picker.find('.check').removeClass('check check-white check-black');
		if (typeof col=='number') color=col; // col is int index
		else if (typeof col=='string'){
			if(col.indexOf('rgb')==0){
				col=col.substring(4,col.lastIndexOf(')'));
				var cA=col.split(',');
				color=colors.in_array('#'+rgb(cA[0].trim(),cA[1].trim(),cA[2].trim())); // col is rgb string, i.e. rgb(12,15,16)
			}
			else color=colors.in_array(col); // col is hex string
		} 
		if (!color && color!=0) return; // color not found
		cp.picker.find('div').eq(color).addClass('check').addClass(isdark(colors[color]) ? 'check-white' : 'check-black');
		cp.conf.callback.call(cp.conf.scope, colors[color]);
	}
	cp.getColor=function(){ var idx=cp.picker.find('div.check'); if (idx) idx=idx.index(); return colors[idx] }
	
   for(i=0;i<clen;i++) 
		if (colors[i]=='-') _html += '<div class="separator"></div>';
		else  _html += '<div style="background-color:'+colors[i]+';"></div>';
	cp.picker=$('<div class="ColorPicker">'+_html+'</div>');
   cp.conf.target.html(cp.picker);
	cp.picker.click(function(el){var $target=$(el.target), color=$target.css('backgroundColor'); cp.setColor(color); });
	cp.setColor(cp.conf.defaultColor);
	return cp;
})



Tasklog.MsgBox=(function(){ return {
	msgTimeout:null, msgboxes:null
	,show: function(msgo){
		var self=this, msgboxID='msg'+(+new Date()), msgbox;
		if (!msgo) return;
		
		var conf={ type:'info', delay:5, autoClose: true }
		if (typeof msgo == 'string') conf.text=msgo
		else $.extend(conf, msgo); 
		
		if(!$('#msgboxes').length) $('body').append('<div id="msgboxes"></div>');
		this.msgboxes=$('#msgboxes');
		
		msgbox = $('<div id="'+msgboxID+'" style="display:none" class="msgbox msgbox-'+conf.type+'"><a class="closer" onclick="msgboxClose(this)" title="Dismiss"></a><span class="msgboxContent">'+conf.text+'</span></div>');
		this.msgboxes.append(msgbox);
		// if autoclose - push close timeout to the mem
		if (conf.autoClose){
			if (!this.msgTimeout) this.msgTimeout={} 
			this.msgTimeout[msgboxID]= setTimeout(function(){ 
				msgbox.fadeOut(function(){$(this).remove()})
				self.msgTimeout[msgboxID]=null;	// remove mem-array entry 
			}, conf.delay*1000)
		}	
		msgbox.fadeIn('slow')
	}
	,close: function(el){
		var box=$(el).parents('.msgbox'), id=box.attr('id');
		if (this.msgTimeout)if (this.msgTimeout[id]){
			clearTimeout(this.msgTimeout[id]); 
			this.msgTimeout[id]=null;
		}	 
		box.fadeOut(function(){$(this).remove()})
	}	
}})();
function msgboxClose(el){ Tasklog.MsgBox.close(el)}
function msgbox(conf){ Tasklog.MsgBox.show(conf) }
function msgerror(txt){ Tasklog.MsgBox.show({type:'error',text:txt}) }




Tasklog.Window=(function(){ return { 
	modal:false, renderCloserDefault: true
	
	,show: function(content, title, modal, renderCloser){
		this.title=title; this.content=content; this.modal=modal;
		if (typeof renderCloser=='boolean') this.renderCloser=renderCloser; else this.renderCloser=this.renderCloserDefault 
		if (this.modal) this.maskShow(); 
		this.windowShow();
		var self=this;
		$(window).bind('resize', function(){self.center.call(self)});
	} 
	
	,hide: function(force){ 
		if(this.wind) if(!this.renderCloser||force){ this.windowHide(); if(this.mask)this.maskHide() }
		$(window).unbind('resize');
	}
	
	,center: function(){
		var l=($('body').innerWidth()/2)-(this.wind.outerWidth()/2), t=($('html').innerHeight()/2)-(this.wind.outerHeight()/2)
		this.wind.css({ left: l<0?l=0:l, top: t<0?t=0:t });
	}
	
	,windowShow: function(){ 
		this.wind=$('#window');
		if (!this.wind.length){ 
			var closer=this.renderCloser?'<div class="window-closer" title="Close" onclick="closeWindow()">X</div>':'';
			this.wind=$('<div id="window">'+closer+'<div class="window-title"></div><div class="window-content"></div></div>'); 
			$('body').append(this.wind); 
			var self=this; 
			this.wind.click(function(e){ e.stopPropagation() }) 
		}
		this.wind.find('.window-title').html(this.title);
		this.wind.find('.window-content').html(this.content);
		
		this.center();
		this.wind.animate({ opacity: 1 })
		this.wind.draggable({ containment: 'body', handle :'.window-title' });
	}
	,windowHide: function(){ var self=this; this.wind.animate({ opacity: 0}, { complete: function(){ self.wind.remove() } })	}
	
	,maskShow: function(){ 
		this.mask=$('#mask');
		if (!this.mask.length){ 
			this.mask=$('<div id="mask"></div>'); 
			$('body').append(this.mask); 
			var self=this; 
			this.mask.click(function(){self.hide.call(self,true)}) 
		}
		this.mask.animate({ opacity: 0.7 }, {  })				
	}	
	,maskHide: function(){ var self=this; if (this.mask) this.mask.animate({ opacity: 0}, { complete: function(){self.mask.remove()}}) }	
}})();
function closeWindow(){ Tasklog.Window.hide(true) }
